home *** CD-ROM | disk | FTP | other *** search
- Path: kbad.eglin.af.mil!rpi!not-for-mail
- From: floydb1@lib104.its.rpi.edu (Barry B Floyd)
- Newsgroups: comp.lang.c++
- Subject: Re: Converting Numbers to English
- Date: 23 Jan 1996 11:32:28 -0500
- Organization: Rensselaer Polytechnic Institute, Troy, NY.
- Message-ID: <4e32is$6u4@lib104.its.rpi.edu>
- References: <4e0pii$grt@mother.usf.edu>
- NNTP-Posting-Host: lib104.its.rpi.edu
- X-newsreader: xrn 7.04-beta-11
-
-
- In article <4e0pii$grt@mother.usf.edu>, oneal@suntan.eng.usf.edu (Aaron Oneal) writes:
- |> Hi all. I need some kind of algorithm to convert a number like
- |> 240,353,740.23 to English (two hundred forty million, three hundred
- |> fifty-three thousand, seven hundred forty and twenty-three
- |> hundredths). I don't really need code, although it certainly couldn't
- |> hurt. I just need a point in the right direction.
- |>
- |> Thanks,
- |> Aaron
- |> -=>Questor<=-
- |>
-
- If using iostreams is ok (i.e. the easiest way I have
- found to convert numbers to 'char *'), a String class and
- a String 'vector' class is ok, then the following may do.
-
- If this is one of your first C/C++ programs, then none of this
- would be of value.
-
- ( You could also use the 'mod' function to get digits. )
-
- Building right to left, first looking for the decimel then looking
- for groups of three (i.e. hundreds) digits would seem to make sense.
-
- (Pseudo)code to the effect ( assumes us if a String class ):
-
- //---------------------------------------------------------------------
- // global variables
- //---------------------------------------------------------------------
-
- StringVec order_of_mag ( ?? ) ; // initialize arrays
- order_of_mag[0] = "" ;
- order_of_mag[1] = " ten" ;
- order_of_mag[2] = " hundred" ;
- order_of_mag[3] = " thousand" ; //etc.
-
- StringVec tens ( 10 ) ;
- tens[0] = "" ;
- tens[1] = "" ;
- tens[2] = " twenty" ;
- tens[3] = " thirty" ; //etc.
-
- StringVec ones ( 10 ) ;
- ones[0] = "" ;
- ones[1] = " one" ;
- ones[2] = " two" ; //etc. thru nineteen
-
- //---------------------------------------------------------------------
- String SetoThree ( String str_number )
- //---------------------------------------------------------------------
- {
- String result = String_EOS ;
-
- if ( str_number.length () == 0 )
- return ( result ) ;
-
- if ( str_number.length () == 3 )
- {
- result += ones[atoi ( (char *)str_number[1]] ;
- result += order_of_mag[2] ;
- }
-
- if ( str_number.length () > 1 )
- {
- if ( atoi ( (char *)str_number[2] ) > 1 )
- result += tens[atoi ( (char*)str_number[2]];
- result += ones[atoi ( (char*)str_number[3]];
- else
- result += ones[atoi ( (char*)str_number.at(2,2)];
- }
- else
- result += ones[ atoi ( (char*)str_number[3]];
-
- return ( result ) ;
- }
-
- //---------------------------------------------------------------------
- String Convert ( String str_num )
- //---------------------------------------------------------------------
- {
- int index = 0 ;
-
- int num_length = str_num.length () ;
-
- int sets_of_three = num_length / 3 ;
-
- int rest = num_length mod 3 ;
-
- if ( rest > 0 )
- {
- String result_str += SetoThree (str_num.at ( 0, rest ) ;
- result_str += order_of_mag[sets_of_three+1] ;
- }
-
- for ( index = 0 ; index < sets_of_three - 1 ; index++ )
- {
- result_str += SetoThree (str_num.at ((3*index)+rest, 3) ;
- result_str += order_of_mag[sets_of_three-index+1] ;
- }
-
- if ( sets_of_three > 0 )
- result_str += SetoThree (str_num.at ( num_length-3, 3 ) ;
-
-
-
- }
-
- //---------------------------------------------------------------------
- //---------------------------------------------------------------------
- main ()
- //---------------------------------------------------------------------
- {
- while ( TRUE ) // type CTRL-C to end program
- {
- cout << "Enter your number: " ;
-
- { // scope for variables and objects
-
- float your_number ;
-
- cin >> your_number ; // get a number
-
- ostrstream strstr_number // convert to String from float
- strstr_number << your_number ;
- String str_number = strstr_number.str () ;
- strstr_number.freeze ( 0 ) ;
-
- int dec_place = str_number.index ( "." ) ;
-
- String english ;
-
- if ( dec_place != -1 ) // no decimel
- {
- english = Convert ( str_number.at ( 0, dec_place ) ) ;
- english += "and" ;
- english += Convert ( str_number.from ( dec_place+1 ) ) ;
- english += order_of_mag[str_number.length()-dec_place-1];
- english += "ths" ;
- }
- else
- english = Convert ( str_number ) ;
-
-
- cout << "Your number in english is: " << endl
- << english << endl ;
-
- } // end scope
- } // end loop
- } // end main
-
- //---------------------------------------------------------------------
- //---------------------------------------------------------------------
- //---------------------------------------------------------------------
- --
- +--------------------------------------------------------------------+
- | Barry B. Floyd \\\ floydb1@rpi.edu |
- | RPI Alum. '84 '87 '88 \\\ |
- +--------------------------------------------------------------------+
-